<template>
  <div>
    <!-- 采购退货金融审核 -->
    <TopFilter :list="topFilterList" :cols="3" @filterChange="changeHandle" @onCollapse="collapseHandle" :labelWidth="110" @resetChange="resetChange"></TopFilter>

    <Spin :spinning="exportLoading" tip="Loading...">
      <FilterTables
        ref="table"
        :columns="columns"
        :fetchapi="fetchApi"
        :params="fetchParams"
        :isSelectColumn="false"
        :datakey="'records'"
        columnsRef="zjFinancialAudit"
        :onRowSelectChange="selectChangeHandle"
        :onColumnsChange="columns => (this.columns = columns)"
      >
        <template slot="controls" slot-scope="props">
          <multiuse-button size="small" type="primary" @click="addHandle(1)">导 出</multiuse-button>
        </template>
      </FilterTables>
    </Spin>

    <BaseDialog :visible.sync="issearch" :title="'经销商查询'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }">
      <ownerSeacher :rowData="formData" @close="ownercloseHandler" />
    </BaseDialog>
    <Drawer :visible.sync="emitvisible" :title="'编辑'" destroyOnClose :containerStyle="{ height: 'calc(100% - 60px)', overflow: 'auto', paddingBottom: '60px' }" width="72%">
      <emitModel :rowData="formData" @close="emitcloseHandler" />
    </Drawer>
  </div>
</template>
<script>
import { getList, exportList, getDealerUser, gettmFiAccountType ,exporturl} from '@/api/omSale/PurchaseReturn/ReturnAudit/index.js';
import { dictionary } from '@/utils/dictMixin';
import { exportExcelFile } from '@/utils';
import emitModel from './emitModel';
import ownerSeacher from './ownerSeacher';
import { getOwnerCode } from '@/assets/js/auth';

export default {
  name: 'TemplateDistribution',
  data() {
    this.BaseTable = null;
    return {
      topFilterList: this.createdFrom(),
      columns: this.createdTable(),
      fetchApi: getList,
      fetchParams: {
        returnStatus: 60551003,
        xhrAbort: true
      },
      formData: {},
      exportLoading: false,
      visible: false,
      emitvisible: false,
      addvisible: false,
      issearch: false,
      isOpen: true,
      selectList: []
    };
  },
  mixins: [dictionary],
  components: {
    emitModel,
    ownerSeacher
  },
  mounted() {
    this.BaseTable = this.$refs.table.$refs.pageTable;
  },
  created() {
    // this.getUser()
    // this.gettmFiAccountType();
  },
  methods: {
    createdFrom() {
      return [
        {
          type: 'INPUT',
          label: '经销商代码',
          fieldName: 'dealerCode',
          placeholder: '请选择',
          unitRender: () => {
            return (
              <div>
                <el-button icon="el-icon-search" onClick={() => this.seacherFun()}></el-button>
              </div>
            );
          }
        },
        {
          type: 'INPUT',
          label: '经销商名称',
          fieldName: 'dealerName',
          placeholder: '请输入'
        },
        {
          type: 'INPUT',
          label: '退货单号',
          fieldName: 'returnNo',
          placeholder: '请输入'
        },
        {
          type: 'RANGE_DATE',
          label: '申请日期',
          fieldName: 'applyDateBegin|applyDateEnd',
          placeholder: '请选择',
          initialValue: [],
          dateType: 'exactdaterange'
        },
        {
          type: 'SELECT',
          label: '动力形式',
          fieldName: 'powerType',
          placeholder: '请选择',
          itemList: this.createDictList('6008')
        },
        {
          type: 'TEXT_AREA',
          label: 'VIN',
          fieldName: 'vins',
          placeholder: '请输入',
          maxlength: '',
          maxRows: 2
        },
        {
          type: 'RANGE_DATE',
          label: '财务审批日期',
          fieldName: 'fiCheckDateBegin|fiCheckDateEnd',
          placeholder: '请选择',
          initialValue: [],
          dateType: 'exactdaterange'
        }
      ];
    },
    createdTable() {
      return [
        {
          title: '序号',
          dataIndex: 'index',
          width: 80,
          sorter: true,
          fixed: 'left',
          render: props => {
            return <span>{props.row.index + 1}</span>;
          }
        },
        {
          title: '操作',
          dataIndex: 'column-action',
          width: 50,
          fixed: 'left',
          render: (props, h) => {
            return (
              <div>
                <multiuse-button size="mini" type="text" onClick={() => this.editHandle(props.row)} disabled={props.row.returnStatus != 60551003}>
                  编辑
                </multiuse-button>
              </div>
            );
          }
        },
        {
          title: '状态',
          dataIndex: 'returnStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6055'),
          render: p => {
            return <span>{this.createDictText(p.row.returnStatus, '6055')}</span>;
          }
        },
        {
          title: '退货单号',
          dataIndex: 'returnNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },

        {
          title: '经销商代码',
          dataIndex: 'dealerCode',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '经销商名称',
          dataIndex: 'dealerName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '申请日期',
          dataIndex: 'applyDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '退车原因',
          dataIndex: 'returnReason',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6056'),
          render: p => {
            return <span>{this.createDictText(p.row.returnReason, '6056')}</span>;
          }
        },
        {
          title: '退货人',
          dataIndex: 'applyPersonName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '动力形式',
          dataIndex: 'powerType',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6008'),
          render: p => {
            return <span>{this.createDictText(p.row.powerType, '6008')}</span>;
          }
        },
        {
          title: '店内联系人',
          dataIndex: 'linkMan',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '联系电话',
          dataIndex: 'linkPhone',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: 'VIN',
          dataIndex: 'vin',
          align: 'center',
          width: 180,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '销售订单号',
          dataIndex: 'doNo',
          sorter: true,
          filter: true,
          filterType: 'input',
          align: 'center',
          width: 200
        },
        {
          title: '产品名称',
          dataIndex: 'productName',
          align: 'center',
          width: 400,
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '原价格',
          dataIndex: 'afterDiscountRebateAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '原发票号',
          dataIndex: 'goldenInvoiceNo',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '原税率',
          dataIndex: 'rate',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '退车金额',
          dataIndex: 'returnAmount',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '退款账户',
          dataIndex: 'returnAccountName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '店端车辆状态',
          dataIndex: 'vehicleStatus',
          sorter: true,
          filter: true,
          filterType: 'checkbox',
          filterItems: this.createDictList('6040'),
          render: p => {
            return <span>{this.createDictText(p.row.vehicleStatus, '6040')}</span>;
          }
        },
        {
          title: '退车原因说明',
          dataIndex: 'returnReasonRemark',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '提交人',
          dataIndex: 'submitPersonName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '提交日期',
          dataIndex: 'submitDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        },
        {
          title: '财务审批人',
          dataIndex: 'fiCheckPersonName',
          sorter: true,
          filter: true,
          filterType: 'input'
        },
        {
          title: '财务审批日期',
          dataIndex: 'fiCheckDate',
          sorter: true,
          filter: true,
          filterType: 'date-range'
        }
      ];
    },
    selectChangeHandle(row) {},
   async addHandle(type) {
       let json = Object.assign({}, { CODE: 'CODE_0000087' }, this.fetchParams);
      let res = await exporturl(json);
      if (res.resultCode == 200) {
        this.$router.push('/admin/auth/oem/downloadCenter');
      }

      // this.$confirm('是否将Excel表格保存至本地文件夹?', '提示', {
      //   confirmButtonText: '下载',
      //   cancelButtonText: '取消',
      //   type: 'warning'
      // })
      //   .then(() => {
      //     let json = this.fetchParams;
      //     this.exportLoading = true;
      //     exportList(json)
      //       .then(response => {
      //         exportExcelFile(response.data, '采购退货金融审核');
      //         this.exportLoading = false;
      //       })
      //       .catch(err => {
      //         console.log(err);
      //       });
      //   })
      //   .catch(() => {
      //     this.$message({
      //       type: 'info',
      //       message: '已取消'
      //     });
      //   });
    },
    changeHandle(params) {
      this.fetchParams.xhrAbort = false;
      this.fetchParams = Object.assign({}, this.fetchParams, params);
    },
    seacherFun() {
      this.issearch = true;
    },
    collapseHandle() {
      this.$nextTick(() => {
        this.BaseTable.EXECUTE_RESET_HEIGHT();
      });
    },
    async gettmFiAccountType(ownerCode) {
      let res = (await gettmFiAccountType({ ownerCode: ownerCode })).data.map(x => ({ text: x.accountTypeName, value: x.id }));
      this.columns.find(x => x.dataIndex == 'returnAccount').editItems = res;
      this.columns.find(x => x.dataIndex == 'returnAccount').filterItems = res;
    },
    editHandle(data) {
      this.formData = data;
      this.emitvisible = true;
    },
    emitcloseHandler(val) {
      if (val) {
        this.isOpen = true;
        this.fetchParams = Object.assign({}, this.fetchParams);
      }
      this.emitvisible = false;
    },
    ownercloseHandler(val, data) {
      if (data) {
        let dealerCodes = [];
        data.map(x => {
          dealerCodes.push(x.dealerCode);
        });
        this.topFilterList.find(x => x.fieldName == 'dealerCode').initialValue = dealerCodes.toString();
      }
      this.issearch = false;
    },
    importcloseHandler() {
      this.visible = false;
      this.isOpen = true;
      this.fetchParams = Object.assign({}, this.fetchParams);
    },
    async getUser() {
      let res = await getDealerUser();
      let empList = res.data.map(x => ({ text: x.employeeName, value: x.userId + '' }));
      this.columns.find(item => item.dataIndex === 'createdBy').editItems = empList; //操作姓名
      this.columns.find(item => item.dataIndex === 'createdBy').filterItems = empList;
    },
    resetChange() {
      this.isOpen = true;
      this.fetchParams = Object.assign({}, this.fetchParams);
    }
  }
};
</script>
